package com.qiall.omsserver.service.dubbo;

import com.qiall.common.entity.PageQuery;
import com.qiall.common.entity.PageResponse;
import com.qiall.common.exception.BusinessResult;
import com.qiall.datautil.RandomUtil;
import com.qiall.oms.ability.FindDubboService;
import com.qiall.oms.ability.request.DubboInfoDTO;
import com.qiall.oms.ability.request.FindDubboServiceRequest;
import com.qiall.oms.ability.request.FindDubboServiceRequestPage;
import com.qiall.oms.ability.request.FindDubboServiceResponse;
import com.qiall.omsserver.data.entity.DubboServiceInfo;
import com.qiall.omsserver.data.mapper.DubboServiceInfoMappr;
import com.qiall.omsserver.service.dubbo.convert.DubboServiceConvert;
import com.qiall.omsserver.service.dubbo.findService.Context;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@DubboService
public class FindDubboServiceImpl implements FindDubboService {

    private static String code = "随机获取";

    @Autowired
    private Context context;

    @Autowired
    private DubboServiceInfoMappr dubboServiceInfoMappr;

    @Autowired
    private DubboServiceConvert dubboServiceConvert;

    @Override
    public FindDubboServiceResponse findBuddoService(FindDubboServiceRequest request) {
        com.qiall.omsserver.service.dubbo.findService.FindDubboService findDubboService = context.getService(code);
        DubboServiceInfo dubboServiceInfo = findDubboService.FindDubboServiceByStrategy(request.getRequestService(), request.getServiceGroup(), request.getVersion());
        FindDubboServiceResponse findDubboServiceResponse = new FindDubboServiceResponse();
        findDubboServiceResponse.setIp(dubboServiceInfo.getIp());
        findDubboServiceResponse.setPort(dubboServiceInfo.getPort());
        return findDubboServiceResponse;
    }


    @Override
    public BusinessResult<PageResponse<DubboInfoDTO>> findBuddoServiceByPage(FindDubboServiceRequestPage request) {
        DubboServiceInfo dubboServiceInfo = new DubboServiceInfo();
        dubboServiceInfo.setApplicationName(request.getApplicationName());
        int count = dubboServiceInfoMappr.count(dubboServiceInfo);
        if (count == 0) {
            return BusinessResult.success(PageResponse.nullValue(request.getPageQuery()));
        }
        PageQuery pageQuery = request.getPageQuery();
        List<DubboServiceInfo> dubboServiceInfos = dubboServiceInfoMappr.selectByPage(dubboServiceInfo, pageQuery.offset(), pageQuery.limit());
        List<DubboInfoDTO> dubboInfoDTOS = dubboServiceInfos.stream().map(x -> dubboServiceConvert.trans(x)).collect(Collectors.toList());
        PageResponse pageResponse = PageResponse.withValue(pageQuery, count, dubboInfoDTOS);
        return BusinessResult.success(pageResponse);

    }

    @Override
    public BusinessResult<DubboInfoDTO> findBuddoServiceByService(String service) {
        List<DubboServiceInfo> dubboServiceInfos = dubboServiceInfoMappr.selectByServiceAndGroup(service, null, "1.0.0");
        DubboInfoDTO dto = dubboServiceConvert.trans(dubboServiceInfos.get(0));
        return BusinessResult.success(dto);
    }
}
